<template>
	<img src="../assets/chen.jpeg" class="logo" />
    <h3>{{ msg }}</h3>
    <p>使用示例请看 {{ $t("nav.example") }}</p>
    <p>
        Recommended IDE setup:
        <a href="https://code.visualstudio.com/" target="_blank">VSCode</a>
        +
        <a
            href="https://marketplace.visualstudio.com/items?itemName=octref.vetur"
            target="_blank"
            >
            Vetur
        </a>
        or
        <a href="https://github.com/johnsoncodehk/volar" target="_blank">Volar</a>
        (if using
        <code>&lt;script setup&gt;</code>)
    </p>
    <p>
        <a href="https://cn.vitejs.dev/" target="_blank">
            Vite Docs
        </a>
        |
        <a href="https://v3.cn.vuejs.org/" target="_blank">Vue 3 Docs</a>
        |
        <a href="https://next.router.vuejs.org/zh/" target="_blank">Vue-router 4.x Docs</a>
        |
        <a href="https://next.vuex.vuejs.org/zh/" target="_blank">Vuex 4.x Docs</a>
        |
        <a href="https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API/Using_Fetch" target="_blank">Fetch</a>
        |
        <a href="https://element-plus.gitee.io/#/zh-CN/component/installation" target="_blank">Element-Plus</a>
    </p>
    <!-- attr, event, computed -->
    <button @click="countPlus">count is: {{ count }}; double count: {{ doubleCount }}</button>
    <!-- type -->
    <h3>类型校验number：{{ bar }}</h3>
    <!-- 联合类型 -->
    <p>str1: {{ (str1 as number).toFixed(2) }}</p>
    <p>year: {{ year }}</p>
</template>

<script setup lang="ts">
import { onMounted, ref, reactive, computed, nextTick } from "vue"

// const props = withDefaults(defineProps<{
//     msg: string
// }>(), {})

// const props = defineProps<{
//     msg: string,
//     bar?: number
// }>()
/** ==等价于== */
interface props {
    msg: string,
    bar?: number
}
defineProps<props>()

// 联合类型
let str1: string | number = 1
// 不设默认值，则结果为联合类型，包括undefined <string | number>
let year = ref<string | number>(2021)
year.value = '2022'

let count = ref(0)
// computed
const doubleCount = computed<number>(() => count.value * 2)
// 事件
async function countPlus (event: Event) {
    console.log('countPlus: ', count.value, ';', (event.target as HTMLButtonElement).innerText)
    count.value = count.value + 1
    await nextTick()
    console.log('nextTick countPlus: ', count.value, ';', (event.target as HTMLButtonElement).innerText)
}

interface Book {
    title: string,
    year?: number
}
// 响应数据
const book1: Book = reactive({
    title: '海底两万里',
    year: 2000
})
// 生命周期
onMounted (() => {
    console.log('书名：', book1.title)
})
</script>

<style scoped>
img {
    width: 200px;
}
a {
    color: #42b983;
}

label {
    margin: 0 0.5em;
    font-weight: bold;
}

code {
    background-color: #eee;
    padding: 2px 4px;
    border-radius: 4px;
    color: #304455;
}
</style>
